# Replication Archive for: 
# Coppock, Alexander and Donald P. Green. 2020. 
# "Do Belief Systems Exhibit Dynamic Constraint?" 
# The Journal of Politics, Forthcoming.


rm(list = ls())

library(tidyverse)
library(haven)

trump_white <- read_sav("data/raw/Trump605/TESS3 174 - Trump_Client.sav")

tw_recode <-
  function(X, flip = NA) {
    X[X == -1] <- NA
    if (!is.na(flip)) {
      X <- -1 * X + flip
    }
    return(X)
  }

# Covariates, Treatment, Outcome ------------------------------------------

trump_white <-
  trump_white %>%
  mutate(
    anon_ID = paste0("trump_white",
                     sprintf("%04d", 1:n())),
    race_2 = fct_collapse(
      factor(PPETHM),
      White = "1",
      `Non-White` = as.character(2:5)
    ),
    race_2 = fct_explicit_na(race_2, na_level = "Non-White"),
    race_text = as_factor(PPETHM),
    race_4 = 
      case_when(
        race_text == "White, Non-Hispanic" ~ "White",
        race_text == "Black, Non-Hispanic" ~ "Black",
        race_text == "Hispanic" ~ "Hispanic",
        TRUE ~ "Other"
      ),
    
    white = as.numeric(race_2 == "White"),
    ideo_3 = fct_collapse(
      factor(ideo),
      Liberal = as.character(1:3),
      Conservative = as.character(5:7),
      `Ideology: Other` = c("-1", "4")
    ),
    ideo_3 = fct_explicit_na(ideo_3, na_level = "Ideology: Other"),
    female_2 = fct_recode(factor(PPGENDER),
                          Female = "2",
                          Male = "1"),
    female_2 = fct_explicit_na(female_2, na_level = "Female"),
    female = as.numeric(female_2 == "Female"),
    educ_3 = fct_collapse(
      factor(PPEDUCAT),
      `High School or Less` = as.character(1:2),
      `Some College` = "3",
      `Bachelor's Degree or Higher` = "4"
    ),
    educ_3 = fct_explicit_na(educ_3, na_level = "Some College"),
    
    educ_text = as.character(as_factor(PPEDUC)),
    educ_5 = 
      case_when(
        educ_text %in% c("No formal education",
                      "1st, 2nd, 3rd, or 4th grade",
                      "5th or 6th grade",
                      "7th or 8th grade",
                      "9th grade",
                      "10th grade",
                      "11th grade",
                      "12th grade NO DIPLOMA") ~ "Less than High School",
        educ_text %in% c("HIGH SCHOOL GRADUATE - high school DIPLOMA or the equivalent (GED)") ~ "High School",
        educ_text %in% c("Some college, no degree", "Associate degree") ~ "Some College",
        educ_text %in% c("Bachelors degree") ~ "College",
        educ_text %in% c("Masters degree", "Professional or Doctorate degree") ~ "Graduate School"
      ),
    
    educ_5 = factor(educ_5, levels = c("Less than High School", "High School", "Some College", "College", "Graduate School")),
    
    college = as.numeric(educ_3 == "Bachelor's Degree or Higher"),
    pid_7fac = factor(party7),
    pid_7 = as.numeric(pid_7fac),
    pid_7n = pid_7,
    pid_3 = fct_collapse(
      pid_7fac,
      Republican = as.character(1:3),
      Independent = "4",
      Democrat = as.character(5:7)
    ),
    tw_Z = if_else(TESS174 %in% 1:3, "treatment", "control"),
    
    tw_manip_1 = as.numeric(Q2 == 1),
    tw_manip_2 = as.numeric(Q3 == 2),
    
    tw_scale_1 =
      tw_recode(QA_1) +
      tw_recode(QA_2) +
      tw_recode(QA_3, 10) +
      tw_recode(QA_4) +
      tw_recode(QA_5) +
      tw_recode(QA_6) +
      tw_recode(QA_7, 10) +
      tw_recode(QA_8),
    
    tw_scale_2 =
      tw_recode(QB_1) +
      tw_recode(QB_2) +
      tw_recode(QB_3) +
      tw_recode(QB_4) +
      tw_recode(QB_5) +
      tw_recode(QB_6),
    
    tw_scale_3 =
      tw_recode(QC_1) +
      tw_recode(QC_2) +
      tw_recode(QC_3) +
      tw_recode(QC_4, 10) +
      tw_recode(QC_5) +
      tw_recode(QC_6, 10) +
      tw_recode(QC_7) +
      tw_recode(QC_8, 10) +
      tw_recode(QC_9, 10) +
      tw_recode(QC_10) +
      tw_recode(QC_11) +
      tw_recode(QC_12, 10) +
      tw_recode(QC_13) +
      tw_recode(QC_14, 10) +
      tw_recode(QC_15),
    
    tw_scale_1_s = tw_scale_1 / sd(tw_scale_1[tw_Z == "Control"], na.rm = TRUE),
    tw_scale_2_s = tw_scale_2 / sd(tw_scale_2[tw_Z == "Control"], na.rm = TRUE),
    tw_scale_3_s = tw_scale_3 / sd(tw_scale_3[tw_Z == "Control"], na.rm = TRUE),
    
    weight = Weight
  )

# Age
trump_white <- within(trump_white, {
  age_3 <- rep(NA, nrow(trump_white))
  age_3[PPAGE <= 39] <- 1
  age_3[PPAGE >= 40 & PPAGE <= 59] <- 2
  age_3[PPAGE >= 60] <- 3
  age_3[is.na(PPAGE)] <- 1
  age_3 <- factor(age_3)
  
  age_5 <- 
    case_when(
      18 <= PPAGE & PPAGE <=29 ~ "18-29",
      30 <= PPAGE & PPAGE <=39 ~ "30-39",
      40 <= PPAGE & PPAGE <=49 ~ "40-49",
      50 <= PPAGE & PPAGE <=59 ~ "50-99",
      60 <= PPAGE  ~ "60+",
    )
})



trump_white <-
  trump_white %>%
  mutate(
    SJ_s =
      (tw_scale_1 - mean(tw_scale_1[tw_Z == "control"], na.rm = TRUE)) /
      sd(tw_scale_1[tw_Z == "control"], na.rm = TRUE),
    ESJ_s = (tw_scale_3 - mean(tw_scale_3[tw_Z == "control"], na.rm = TRUE)) /
      sd(tw_scale_3[tw_Z == "control"], na.rm = TRUE)
  )




# Checks ------------------------------------------------------------------

# No Covariate Missingness
table(trump_white$age_3, useNA = "always")
table(trump_white$race_2, useNA = "always")
table(trump_white$ideo_3, useNA = "always")
table(trump_white$female_2, useNA = "always")
table(trump_white$educ_3, useNA = "always")
table(trump_white$pid_7, useNA = "always")
table(trump_white$pid_3, useNA = "always")

# No Condition Missingness
table(trump_white$Z, useNA = "always")

# Outcomes
table(trump_white$Y, useNA = "always")

# Save datasets -----------------------------------------------------------

trump_white <-
  trump_white %>%
  zap_labels()

trump_white <-
  trump_white %>%
  select(
    anon_ID,
    age_3,
    age_5,
    race_2,
    race_4,
    white,
    ideo_3,
    female_2,
    female,
    educ_3,
    educ_5,
    college,
    pid_7,
    pid_7n,
    pid_3,
    weight,
    starts_with("tw_"),
    SJ_s,
    ESJ_s
  )

write_rds(trump_white, path = "data/clean/original_trump_white_clean.rds")
